local chongde = fk.CreateSkill{
    name = "jinhs__chongde",
}

Fk:loadTranslationTable{
    ["jinhs__chongde"] = "崇德",
    [":jinhs__chongde"] = "出牌阶段开始时，若你不处于连环状态，你可以与一名其他势力角色各弃置至多X张牌并摸等量张牌，然后你横置(X为你与其的手牌数之差且至多为你的体力上限)。",

    ["#jinhs__chongde-choose"] = "崇德：选择一名其他势力角色",
    ["#jinhs__chongde_discard"] = "崇德：选择弃置至多%arg张牌并摸等量张牌",

    ["$jinhs__chongde1"] = "此吾之诚意，绝非诡诈之计。",
    ["$jinhs__chongde2"] = "以信取吴人之心，则一举而成。",
}

local H = require "packages/ol_hegemony/util"

chongde:addEffect(fk.EventPhaseStart,{
    anim_type = "drawcard",
    can_trigger = function (self, event, target, player, data)
       local targets = table.filter(player.room.alive_players, function (p) return p ~= player and (not H.compareKingdomWith(p, player) or p.kingdom == "unknown")
       and not p:isNude() end)
       return target == player and player.phase == Player.Play and player:hasSkill(chongde.name) and not player.chained
       and #targets > 0 and not player:isNude()
    end,
    on_cost = function (self, event, target, player, data)
       local room = player.room
       local targets = table.filter(room.alive_players, function (p) return p ~= player and (not H.compareKingdomWith(p, player) or p.kingdom == "unknown")
       and not p:isNude() end)
       if #targets == 0 then return false end
       local to = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#jinhs__chongde-choose",
        skill_name = chongde.name,
       })
       if #to > 0 then
        event:setCostData(self, { tos = to})
        return true
      end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local to = event:getCostData(self).tos[1]
        local num = math.abs(player:getHandcardNum() - to:getHandcardNum())
        local n = math.min(num, player.maxHp)
        if not player:isNude() or player:isAlive() then
        local cards1 = room:askToCards(player,{
            min_num = 1,
            max_num = n,
            include_equip = true,
            prompt = "#jinhs__chongde_discard:::"..n,
            skill_name = chongde.name,
            cancelable = false,
        })
        if #cards1 > 0 then
           room:throwCard(cards1, chongde.name, player, player)
           player:drawCards(#cards1, chongde.name)
        end
      end
       if not to:isNude() or to:isAlive() then
        local cards2 = room:askToCards(to,{
            min_num = 1,
            max_num = n,
            include_equip = true,
            prompt = "#jinhs__chongde_discard:::"..n,
            skill_name = chongde.name,
            cancelable = false,
        })
        if #cards2 > 0 then
           room:throwCard(cards2, chongde.name, to, to)
           to:drawCards(#cards2, chongde.name)
        end
      end
        if not player.chained then
        player:setChainState(true)
      end
    end,
})

return chongde